package com.um2.mauction.java;

import java.io.IOException;
import java.util.Iterator;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.DynaActionForm;

import com.um2.mauction.entity.Enchere;
import com.um2.mauction.entity.Utilisateur;
import com.um2.mauction.entity.Vente;
import com.um2.mauction.session.impl.EnchereManagerBean;
import com.um2.mauction.session.impl.UserManagerBean;
import com.um2.mauction.session.impl.VenteManagerBean;
import com.um2.mauction.session.remote.EnchereManagerRemote;
import com.um2.mauction.session.remote.UserManagerRemote;
import com.um2.mauction.session.remote.VenteManagerRemote;

public class PlaceBidInsertAction extends Action{

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		ActionForward resultat = mapping.findForward("succes");
		ActionErrors errors = new ActionErrors();

		float newBid =Float.parseFloat((String) ((DynaActionForm) form).get("txtNewBid")) ;
		//int idVente = (int) request.getAttribute("id_sale");
		
		EnchereManagerRemote bidmanager = null;
		VenteManagerRemote salemanager = null;
		UserManagerRemote usermanager = null;
		
		int id_Sale = 2; // paramètre
		int id_Bidder=1; // id_user - session
		

		try {
			InitialContext ic = new InitialContext();

			bidmanager = (EnchereManagerRemote) ic
					.lookup(EnchereManagerBean.RemoteJNDIName);
			salemanager = (VenteManagerRemote) ic
			.lookup(VenteManagerBean.RemoteJNDIName);
			
			usermanager = (UserManagerRemote) ic
			.lookup(UserManagerBean.RemoteJNDIName);
			
			// récupération objets persistés (detached objects)
			
			Vente sale = salemanager.getVente(id_Sale);
			Utilisateur bidder = usermanager.getUtilisateur(id_Bidder);
			
			
			// Création de l'objet enchère
			//Enchere e = new Enchere(newBid,bidder,sale);
			
			//bidmanager.addEnchere(e);
			bidmanager.addEnchere(newBid,bidder,sale);
			// Associations
			
			//bidder.getBids().add(e);
			//sale.getBids().add(e);
					
			// merge
					
			//salemanager.mergeVente(sale);
			//usermanager.mergeUtilisateur(bidder);
			
						
			for (Iterator<Enchere> it = sale.getBids().iterator(); it.hasNext();){
				System.out.println(((Enchere) it.next()).getBid_id()+"%");	
			}
			
					
			for (Iterator<Enchere> it = bidder.getBids().iterator(); it.hasNext();){
				System.out.println(((Enchere) it.next()).getBid_id()+"$");	
			}
			
			request.getSession().removeAttribute("txtOldBid");
			request.getSession().removeAttribute("txtNewBid");
			
		} catch (NamingException e) {
			errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("ajoutprod.statefulBean.notBound"));
			resultat = new ActionForward(mapping.getInput());
		}

		if (!errors.isEmpty()) {
			saveErrors(request, errors);
		}
		return resultat;

	}
}
